/*
 * DownloadPage.java
 *
 * Created on February 3, 2009, 1:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package regnum_web_trade;

import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 *
 * @author alvaro
 */
class DownloadPage extends Thread {
    private String page;
    private String dest;
    private boolean finished = false;
    private int error = 0;
    private javax.swing.JEditorPane editor;
    private String prefix;
    private String s = System.getProperty("file.separator");
    
    /** Creates a new instance of DownloadPage */
    public DownloadPage(String prefix, String page, String dest, javax.swing.JEditorPane editor) {
        this.page = page;
        this.dest = dest;
        this.editor = editor;
        this.prefix = prefix;
    }
    
    public void run(){
        try{
            URL u = new URL(page);
            
            URLConnection url = u.openConnection();
            
            Scanner scan = new Scanner(new InputStreamReader(url.getInputStream()));
            PrintWriter pw = new PrintWriter(new FileWriter(new File(dest)));
            
            while(scan.hasNextLine()){
                pw.println(scan.nextLine());
            }
            
            scan.close();
            pw.close();
            
            this.finished = true;
            
            CleanWebPage clean = new CleanWebPage(this.prefix, prefix+s+"temp"+s+"trade.html");
            clean.clean();
            this.editor.setPage(new File(prefix+s+"null.html").toURI().toURL());
            this.editor.setPage(new File(this.prefix+this.s+"temp"+s+"trade.html").toURI().toURL());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    public boolean getFinished(){
        return this.finished;
    }
    
    public int getError(){
        return this.error;
    }
    
    public static void main(String arg[]) throws Exception {
        /*DownloadPage down = new DownloadPage("http://code.google.com/p/rolgps/downloads/list?q=label:Plugin", "temp/page1.html");
        down.download();*/
    }
    
}

